# -*- coding: utf-8 -*-

'''eof
name:申请人存量业务当前逾期天数校验
code:R56_PersonalInventoryBusinessMinOvduDaysCheck
tableName:
columnName:
groups:业务其他规则校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def get_age(str1):
    try:
        date1 = datetime.now()
        date2 = date_format(str1)
        diff_year = date1.year - date2.year
        diff_month = date1.month - date2.month
        diff_day = date1.day - date2.day
        age = diff_year + diff_month/12.0 + diff_day/365.0
        return age
    except:
        return u'缺失值'


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d')
                except:
                    date = None
    return date


def float_check(target):
    try:
        float(target)
        return True
    except:
        return False


def getRule56():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        abnormalloan = json.loads(ratingRequest.get("abNormalloan"))
        if abnormalloan not in null_type_list and isinstance(abnormalloan, list):
            if len(abnormalloan) >= 1:
                duplicate_list = []
                flag = False
                for i in abnormalloan:
                    overdueday = i.get("overdueDay")
                    customertype = i.get("customerType")
                    businessType = i.get("businessType")
                    if customertype not in null_type_list and float_check(overdueday):
                        flag = True
                        if customertype == "0":
                            if businessType in ("107010", "11104010", "10101025"):
                                duplicate_list.append(0)
                                continue
                            duplicate_list.append(float(overdueday))
                if flag:
                    if len(duplicate_list) > 0:
                        return max(duplicate_list)
                    else:
                        return 0
                else:
                    raise ValueError("网贷传输的数据字段缺失或不符合要求")
            else:
                return 0
        else:
            raise RuntimeError("网贷传输的数据格式错误或者为空")
    except:
        raise


if __name__ == "__main__":
    ratingRequest = {"legalPersonIdCard":"500101199202192411","timeLimit":"12","abNormalloan": [{"overdueDay": "30", "customerType":"1"}]}
    aa = getRule56()
    print aa


result = getRule56()